﻿using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.Configuration;
using BookShop.DTO;

namespace BookShop.Library.Common
{
    public class CommonFunction
    {
        public static bool CheckModeratorRole(Moderator moderator, int role)
        {
            if (moderator == null)
            {
                return false;
            }
            var strRoleList = moderator.Role.Split(',');
            return strRoleList.Contains(role.ToString("D"));
        }

        public static string EncryptString(string yourString)
        {
            var md5Helper = new MD5CryptoServiceProvider();
            var hashBytes = Encoding.UTF8.GetBytes(yourString);
            hashBytes = md5Helper.ComputeHash(hashBytes);
            var strBuilder = new StringBuilder();
            foreach (var hashByte in hashBytes)
            {
                strBuilder.Append(hashByte.ToString("x2").ToLower());
            }
            return strBuilder.ToString();
        }

        public static string GetConfigValue(string key)
        {
            return WebConfigurationManager.AppSettings[key];
        }
    }
}
